/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.domain.service;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.domain.po.Button;
import org.cshoo.tattoo.authorize.domain.po.Menu;
import org.cshoo.tattoo.authorize.domain.repository.IButtonRepository;
import org.cshoo.tattoo.embed.infra.utils.AuditFieldsUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
public class ButtonService {

    @Resource
    private IButtonRepository buttonRepository;

    /**
     * 判断菜单编号是否存在
     *
     * @return 判断结果
     */
    public boolean buttonCodeExists(String code, Long menuId) {
        Menu menu = new Menu();
        menu.setId(menuId);
        Button button = buttonRepository.findByCodeAndMenuId(code, menuId);
        return Objects.nonNull(button);
    }

    /**
     * 添加一个button
     *
     * @param button 按钮
     */
    public void addButton(Button button) {
        log.info("button:{}", button);
        AuditFieldsUtils.setAddAuditFields(button);
        buttonRepository.save(button);
    }

    /**
     * 修改一个button
     *
     * @param button 按钮
     */
    public void updateButton(Button button) {
        log.info("button:{}", button);
        AuditFieldsUtils.setUpdateAuditFields(button);
        buttonRepository.updateButton(button);
    }

    /**
     * 删除一个button
     *
     * @param id id
     */
    public void deleteButton(Long id) {
        log.info("id:{}", id);
        buttonRepository.deleteById(id);
    }

    /**
     * 根据菜单查询button，即查询这个菜单下的buttons
     *
     * @param menuId 菜单id
     * @return buttons
     */
    public List<Button> findButtonsByMenuId(Long menuId) {
        log.info("menuId:{}", menuId);
        return buttonRepository.findButtonsByMenuId(menuId);
    }

    /**
     * 根据角色id查询按钮
     *
     * @param roleId 角色id
     * @return 按钮
     */
    public List<Button> findByRoleId(Long roleId) {
        log.info("roleId:{}", roleId);
        return buttonRepository.findByRoleId(roleId);
    }

}
